﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.Business;
using AMM.Business.Store;
using AMM.Model;
using AMM.Model.Project;
using AMM.Model.Store;

namespace AMM.UserControls {
    public partial class MouldSearchControl : BaseSearchControl {

        public override BaseSearchModel GetSearchModel() {
            int mouldTypeValue = Convert.ToInt32(this.cmbMouldType.SelectedValue);
            int vendorValue = Convert.ToInt32(this.ddlVendorPlace.SelectedValueEx);

            MouldSearchModel searchResult = new MouldSearchModel();
            searchResult.MouldFrameworkCode = this.tbMouldFrameworkCode.Text;
            searchResult.Code = this.tbCode.Text;
            searchResult.Name = this.tbName.Text;
            searchResult.MouldTypeId = mouldTypeValue;
            searchResult.VendorId = vendorValue;

            if (mouldTypeValue == 0) { searchResult.MouldTypeId = null; }
            if (vendorValue == 0) { searchResult.VendorId = null; }

            return searchResult;
        }

        public override void ResetSearchModel() {
            this.tbMouldFrameworkCode.Clear();
            this.tbCode.Clear();
            this.tbName.Clear();
            this.cmbMouldType.SelectedIndex = 0;
            this.ddlVendorPlace.SelectedValueEx = null;
        }

        public MouldSearchControl() {
            this.InitializeComponent();
        }

        private void BindVendor() {
            VendorSearchModel searchModel = new VendorSearchModel() { IsEnabled = true };
            VendorProcessor customerProcessor = new VendorProcessor(int.MaxValue);
            List<VendorViewModel> customerList = customerProcessor.FetchVendorViewModelList(searchModel).ToList();

            this.ddlVendorPlace.DisplayMemberEx = "Code";
            this.ddlVendorPlace.ValueMemberEx = "Key";
            this.ddlVendorPlace.HeightEx = 300;
            this.ddlVendorPlace.WidthEx = 400;
            this.ddlVendorPlace.PageSizeEx = 20;
            this.ddlVendorPlace.DataBind(new VendorViewModel(), x => new { x.Code, x.Name, x.Address }, customerList);
        }

        private void MouldSearchControl_Load(object sender, EventArgs e) {
            this.BindVendor();
            this.BindBasics(this.cmbMouldType, BasicsType.MouldType);
        }

    }
}
